package org.lanqiao.service.impl;


import org.lanqiao.dao.UserDao;
import org.lanqiao.entity.User;
import org.lanqiao.mapper.UserMapper;
import org.lanqiao.service.UserService;
import org.lanqiao.util.Result;
import org.lanqiao.util.SessionListener;
import org.lanqiao.util.UUId;
import org.lanqiao.util.judgmentEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    Result result;

    @Override
    public List<User> queryUserList() {
        return userDao.queryUserList();
    }

    /**
     * @return org.lanqiao.util.Result
     * @Author 刘金渝
     * @Description //TODO
     * @Date 2019/5/27 17:08
     * @Param [userName]
     * @explain
     */

    @Override
    public Result selectByName(String userName) {
        try {
            User user = userDao.selectByName(userName);
            return Result.ok(user);
        } catch (Exception e) {
            return Result.defeated("");
        }
    }

    /**
     * @return java.lang.String
     * @Author 李玉鑫
     * @Description //TODO
     * @Date 2019/5/22 15:06
     * @Param 增加用户
     */
    @Override
    public Result addUser(User user) {
        String pwd = user.getUserPassword();
        pwd = pwd.replace(" ", "+");
        //密码的正则表达式
        Pattern pattern = Pattern.compile("^(?![A-Z]*$)(?![a-z]*$)(?![0-9]*$)(?![^a-zA-Z0-9]*$)\\S+$");
        Matcher matcher = pattern.matcher(pwd);
        boolean b = matcher.matches();
        //用户名的正则表达式
        Pattern pattern1 = Pattern.compile("^[a-zA-Z0-9\u4E00-\u9FA5]+$");
        Matcher matcher1 = pattern1.matcher(user.getUserName());
        boolean b1 = matcher1.matches();
        if (!judgmentEmail.isEmail(user.getUserEmail())) {
            return Result.defeated("邮箱格式不正确");
        }
        try {
            user.setUserId(UUId.getUUID());

            if (user.getUserName().length() < 1 || user.getUserName().length() > 6 || user.getUserEmail() == "" || pwd == "" || user.getUserName() == "" || b == false || b1 == false || pwd.length() < 6 || pwd.length() > 12) {
                return Result.defeated("");
            } else {
                userDao.addUser(user);
                return Result.ok("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return org.lanqiao.entity.Result
     * @Author 王宇
     * @Description //TODO 通过邮箱密码查询用户(用于登录)
     * @Date 2019/05/23 9:39
     * @Param [userEmail, userPassword]
     */
    @Override
    public Result getUser(String userEmail, String userPassword) {
        try {
            User user = userDao.getUser(userEmail, userPassword);
            System.out.println(user.getUserStatus());
            if(1==user.getUserStatus()){
                return Result.defeated("账号异常");
            }else{
            if (user != null) {
                if (SessionListener.sessionMap.get(userEmail) != null) {
                    return Result.defeated("账户已登陆");
                } else {
                    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                    HttpSession session = request.getSession();
                    session.setAttribute(userEmail, userEmail);
                    SessionListener.sessionCreated(session, userEmail);
                    result.setCode(0);
                    result.setData(user.getUserId());
                    result.setMsg("邮箱密码输入正确,登录成功");
                }
            } else {
                result.setCode(1);
                result.setMsg("邮箱密码输入错误,请重试");
            }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * @return org.lanqiao.util.Result
     * @Author 徐金星
     * @Description //TODO
     * @Date 2019/5/23 17:47
     * @Param [id]
     * @explain
     */
    @Override
    public Result updateUserStatusF(String id) {
        try {
            userDao.updateUserStatusF(id);
            return result.ok("");
        } catch (Exception e) {
            return result.defeated("");
        }


    }

    @Override
    public Result updateUserStatusJ(String id) {
        try {
            userDao.updateUserStatusJ(id);
            return result.ok("");
        } catch (Exception e) {
            return result.defeated("");
        }
    }

    /**
     * 任春旭：查询用户信息
     *
     * @return
     */
    @Override
    public Result queryUser(String userEmail) {
        try {
            User user = userDao.queryUser(userEmail);

            if (user != null) {
              return   result.ok(user);
            } else {
              return   result.defeated("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    /**
     * 任春旭：修改用户昵称
     *
     * @param userName
     * @param userId
     */
    @Override
    public Result upName(String userName, String userId) {
        User user = userDao.upName(userName, userId);
        Result r = new Result<>();
        try {
            if (user != null) {
               return result.ok(user);
            } else {
               return result.defeated("");
            }
        } catch (Exception e) {
           result.defeated("");
        }
        return null;
    }


    /**
     * 任春旭：修改用户邮箱
     *
     * @param userEmail
     * @param userId
     */
    @Override
    public Result upEmail(String userEmail, String userId) {
        User user = userDao.upEmail(userEmail, userId);
        try {
            if (user != null){
                return result.ok(user);
            }else {
               return result.defeated("");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 任春旭：修改用户密码
     *
     * @param userPassword
     * @param userId
     */
    @Override
    public Result upPassword(String userPassword, String userId) {
        try {
        User user = userDao.upPassword(userPassword, userId);
        if (user != null){
            return result.ok(user);
        }else {
            return result.defeated("");
        }
        }catch (Exception e){
         e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     * @Author 李玉鑫
     * @Description //TODO
     * @Date 2019/6/5 11:11
     * @Param 退出
     */
    public Result userExit(String userEmail) {
        SessionListener.sessionDestroyed(userEmail);
        return Result.ok("");
    }

}
